import React, {PureComponent} from "react";
import FormEditor from '../../components/FormEditor';
import {updateOrderFollows} from "./actions";
import {connect} from "react-redux";
import {formatStrToDate} from "../../lib/func";
import lodash from 'lodash';

class OrderFollowModal extends PureComponent {

    handleUpdateFollowModel = follow => {
        const {dispatch, follows, order} = this.props;
        let deleteIndex = follows.findIndex(f => f.id === follow.id);
        follows.splice(deleteIndex, 1, follow);
        if (!lodash.isNil(order.actDepartureTime) && !lodash.isNil(follows[0].arriveTime)) {
            // 算出时效
            const date1 = formatStrToDate(order.actDepartureTime);
            const date2 = formatStrToDate(follows[0].arriveTime);
            follows[0].aging = date2.diff(date1, 'day');
        }
        dispatch(updateOrderFollows(follows));
    };

    handleShowAdd = () => {
        const {dispatch, follows} = this.props;
        follows.push({"id": Math.random().toString()});
        dispatch(updateOrderFollows(follows));
    };

    deleteRow = target => {
        const {dispatch, follows} = this.props;
        let deleteIndex = follows.findIndex(item => item.id === target);
        follows.splice(deleteIndex, 1);
        dispatch(updateOrderFollows(follows));
    };

    render() {
        const {follows, order, orderFollowStatusList} = this.props;
        // 检查是否有费用锁定
        let feeHasLock = false;
        order.feeList && order.feeList.forEach(item => {
            if (item.status === "LOCK") {
                feeHasLock = true;
            }
        });
        order.outFeeList && order.outFeeList.forEach(item => {
            if (item.status === "LOCK") {
                feeHasLock = true;
            }
        });
        const schema = [
            {
                title: '柜号信息',
                showExtra: false,
                func: this.deleteRow,
                target: "id",
                fields: [
                    {
                        field: 'cabinetNumber',
                        title: '柜号',
                        readonly: feeHasLock,
                        type: 'text'
                    }, {
                        field: 'packagePic',
                        title: '装箱图片',
                        readonly: feeHasLock,
                        type: 'autoComplete',
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'customsFile',
                        title: '报关文件',
                        type: 'autoComplete',
                        readonly: feeHasLock,
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'carFile',
                        title: '随车文件',
                        readonly: feeHasLock,
                        type: 'autoComplete',
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'packageSolution',
                        title: '装箱方案',
                        type: 'autoComplete',
                        readonly: feeHasLock,
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'stFile',
                        title: 'ST文件',
                        type: 'autoComplete',
                        readonly: feeHasLock,
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'preOrderConfirm',
                        title: '预录单确认',
                        readonly: feeHasLock,
                        type: 'autoComplete',
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'transportOrder',
                        title: '运单',
                        type: 'autoComplete',
                        readonly: feeHasLock,
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'customsReleaseOrder',
                        title: '报关放行单',
                        readonly: feeHasLock,
                        type: 'autoComplete',
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'arriveTime',
                        title: 'ATA到达日期',
                        readonly: order.settlement === "已结算",
                        type: 'date'
                    }, {
                        field: 'dskAtbMrn',
                        title: 'DSK/ATB/MRN',
                        type: 'autoComplete',
                        readonly: order.settlement === "已结算",
                        controlProps: {
                            dataSource: orderFollowStatusList
                        }
                    }, {
                        field: 'aging',
                        title: '时效/天',
                        readonly: true,
                        type: 'text'
                    }
                ]
            }];

        return (
            <div>
                {follows && follows.map(item => (
                    <FormEditor
                        schema={schema}
                        column={3}
                        key={item.id}
                        defaultReadonly={order.status === "CONFIRMED"}
                        showActionBar={false}
                        defaultValues={item}
                        onChange={this.handleUpdateFollowModel}
                    />
                ))}
            </div>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.zadOrder.orderFollows,
        order: state.zadOrder.modify.model,
        clientList: state.zadOrder.list.clientList,
        carrierList: state.zadOrder.list.carrierList,
        orderFollowStatus: state.common.orderFollowStatus,
        orderFollowStatusList: state.common.orderFollowStatusList
    };
};

export default connect(mapStateToProps)(OrderFollowModal);
